package com.cskaoyan.order.biz.handler;

import com.cskaoyan.order.biz.callback.SendEmailCallback;
import com.cskaoyan.order.biz.callback.TransCallback;
import com.cskaoyan.order.biz.context.CreateOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import com.cskaoyan.order.constant.OrderConstants;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.CartProductDto;
import com.cskaoyan.order.utils.GlobalIdGeneratorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 初始化订单 生成订单
 *
 * @author: 叶熠彬
 * @date: 2022/7/29 21:50
 * @param:
 * @return:
 */
@Slf4j
@Component
public class InitOrderHandler extends AbstractTransHandler {


    @Autowired
    SendEmailCallback sendEmailCallback;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;


    @Autowired
    GlobalIdGeneratorUtil globalIdGeneratorUtil;


    private final String ORDER_GLOBAL_ID_CACHE_KEY = "ORDER_ID";
    private final String ORDER_ITEM_GLOBAL_ID_CACHE_KEY = "ORDER_ITEM_ID";

    @Override
    public TransCallback getTransCallback() {
        return sendEmailCallback;
    }

    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {
        // 创建order对象
        Order order = new Order();
        CreateOrderContext createOrderContext = (CreateOrderContext) context;
        // 发号器生成orderID
        String orderId = globalIdGeneratorUtil.getNextSeq(ORDER_GLOBAL_ID_CACHE_KEY, 1);
        order.setOrderId(orderId);
        order.setPayment(createOrderContext.getOrderTotal());
        order.setStatus(OrderConstants.ORDER_STATUS_INIT);
        Date date = new Date();
        order.setCreateTime(date);
        order.setUpdateTime(date);
        order.setUserId(createOrderContext.getUserId());
        order.setBuyerNick(createOrderContext.getBuyerNickName());
        order.setBuyerComment(0);
        // 插入order表
        orderMapper.insertSelective(order);


        List<CartProductDto> cartProductDtoList = createOrderContext.getCartProductDtoList();
        List<Long> productsId = new ArrayList<>();

        for (CartProductDto cartProductDto : cartProductDtoList) {
            OrderItem orderItem = new OrderItem();
            // 生成订单商品唯一索引
            String orderItemId = globalIdGeneratorUtil.getNextSeq(ORDER_ITEM_GLOBAL_ID_CACHE_KEY, 1);
            orderItem.setId(orderItemId);
            orderItem.setItemId(cartProductDto.getProductId());
            orderItem.setOrderId(orderId);
            orderItem.setNum(cartProductDto.getProductNum().intValue());
            orderItem.setPrice(cartProductDto.getSalePrice().doubleValue());
            orderItem.setTitle(cartProductDto.getProductName());
            orderItem.setPicPath(cartProductDto.getProductImg());
            orderItem.setStatus(OrderConstants.ORDER_ITEM_STOCK_STATUS_LOCK);
            orderItem.setCreate(new Timestamp(date.getTime()));
            // 插入order_item表
            orderItemMapper.insertSelective(orderItem);
            // 生成订单商品的ID list
            productsId.add(cartProductDto.getProductId());
        }

        // 更新context
        ((CreateOrderContext) context).setOrderId(orderId);
        ((CreateOrderContext) context).setBuyProductIds(productsId);
        return true;
    }

}
